package cn.lixufeng.pay.service.impl;

import cn.lixufeng.basic.exception.BusinessException;
import cn.lixufeng.basic.service.impl.BaseServiceImpl;
import cn.lixufeng.order.service.IProductOrderService;
import cn.lixufeng.pay.domain.AlipayInfo;
import cn.lixufeng.pay.domain.PayBill;
import cn.lixufeng.pay.mapper.AlipayInfoMapper;
import cn.lixufeng.pay.mapper.PayBillMapper;
import cn.lixufeng.pay.service.IPayBillService;
import cn.lixufeng.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }

    @Override
    public String pay(PayBill paybillTmp) {
        // 校验
        if(paybillTmp == null){
            throw new BusinessException("支付数据为空！");
        }
        PayBill paybill = payBillMapper.findById(paybillTmp.getId());
        if(paybill == null){
            throw new BusinessException("支付数据为空！");
        }
        // switch/case根据支付类型
        switch (paybill.getPayChannel().intValue()){
            case 1 :// 支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(paybill.getShop_id());
                return AlipayUtils.pay(info, paybill);
            case 2 :// 微信

                break;
            case 3 :// 银联

                break;
            default : //余额

        }
        return null;
    }
}
